import os
from tqdm import tqdm
import json
from basic_component_related_management.models import *
from control.models.User import User
from unit.models import *
from literature.models import *
from dataset.models import *
from django.db import IntegrityError, transaction
import traceback


# 创建一个superadmin，密码是superadmin1234
user_entity = User.objects.filter(user_account="zfh20").first()
if user_entity is None:
    user_entity = User.objects.create(
        user_account="zfh20",
        user_name="张飞鸿",
        password="DT/+UnpNa3VNzi1sUIKqD66vEKr3SWkIL/ewqe50uDPNPLzOvf8JKV+LFFiFvWTCHR3851FQO+qw4ToXQdCJGk/UcvgduTHoVkRelYkj+2E7EisSNMOfKuIg00RGRip5IdtKB3oQ1vE6yV8nyv8CT7CNrNOcN5BFNDQ/un7AXFxU9j4ZurcDwWLFwiH2Zzd02lNz2gx9fL0zWMniQ502h4AH5RnsukG9sTBMmQTcuo41ZDGwpsnQDmdz6rh84vGEoWg/sYijTlfb25fpOxp29m3u0gO6Rbnqt7X2TZMb4SZud+dEk3Zy2NO9nWIO/VGPUE5a2LfXhGuYcy/MPwDqKw=="
    )

# 创建单位的基本内容
dimension_less_entity = Dimension.objects.filter(
    dimension_id="D_Dimensionless").first()
if dimension_less_entity is None:
    dimension_less_entity = Dimension.objects.create(
        dimension_name="无量纲",
        dimension_name_en="dimensionless",
        dimension_id="D_Dimensionless",
        basic_unit="1",
        description="无量纲",
        create_person=user_entity,
        modify_person=user_entity,
    )
unit_entity = Unit.objects.filter(symbol="1").first()
if unit_entity is None:
    unit_entity = Unit.objects.create(
        unit_name="无量纲",
        unit_name_en="dimensionless",
        symbol="1",
        dimension=dimension_less_entity,
        is_system = True,
        create_person=user_entity,
        modify_person=user_entity,
    )
    UnitSymbol.objects.create(
        symbol="1",
        root_unit=unit_entity,
        is_system = True,
        create_person=user_entity,
        modify_person=user_entity,
    )
# 创建量
quantity_entity = Quantity.objects.filter(quantity_name="nist量").first()
if quantity_entity is None:
    quantity_entity = Quantity.objects.create(
        quantity_name="nist量",
        quantity_name_en="nist_quantity",
        quantity_type="NORMAL",
        dimension=dimension_less_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    QuantityUnitMap.objects.create(
        quantity=quantity_entity,
        unit=unit_entity)
# 文献部分的单位
quantity_purity_entity = Quantity.objects.filter(quantity_name="样品纯度").first()
if quantity_purity_entity is None:
    quantity_purity_entity = Quantity.objects.create(
        quantity_name="样品纯度",
        quantity_name_en="sample_purity",
        quantity_type="NORMAL",
        dimension=dimension_less_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    QuantityUnitMap.objects.create(
        quantity=quantity_purity_entity,
        unit=unit_entity)
percent_unit_entity = Unit.objects.filter(symbol="%").first()
if percent_unit_entity is None:
    percent_unit_entity = Unit.objects.create(
        unit_name="百分比",
        unit_name_en="percent",
        symbol="%",
        is_system = True,
        dimension=dimension_less_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    UnitConversion.objects.create(
        base_unit=unit_entity,
        target_unit=percent_unit_entity,
        factor=100,
        offset=0,
        create_person=user_entity,
        modify_person=user_entity,
    )
    UnitSymbol.objects.create(
        symbol="%",
        is_system = True,
        root_unit=percent_unit_entity,
        create_person=user_entity,
        modify_person=user_entity,
    )
    QuantityUnitMap.objects.create(
        quantity=quantity_purity_entity,
        unit=percent_unit_entity)


def store_compound_attribute(compound1, **compound_item):
    if "inchikey" in compound_item:
        attribute_entity = Attribute.objects.filter(
            main_dataIndex="system_inchikey").first()
        if attribute_entity is None:
            attribute_entity = Attribute.objects.create(
                main_name="InChIKey",
                main_name_en="InChIKey",
                main_dataIndex="system_inchikey",
                main_datatype="short_string",
                main_necessary=False,
                common_used=True,
                comment="系统初始化,InChIKey",
                multiple_value_used=False,
                is_system_config=True,
                create_person=user_entity,
                modify_person=user_entity,
            )
        ComponentAttributeValue.objects.create(
            componentidentifier=compound1,
            attribute=attribute_entity,
            main_value=json.dumps(compound_item["inchikey"]),
        )

    if "other_name" in compound_item:
        attribute_entity = Attribute.objects.filter(
            main_dataIndex="system_other_name").first()
        if attribute_entity is None:
            attribute_entity = Attribute.objects.create(
                main_name="其他名称",
                main_name_en="Other Name",
                main_dataIndex="system_other_name",
                main_datatype="short_string",
                main_necessary=False,
                common_used=True,
                comment="系统初始化,其他名称",
                multiple_value_used=True,
                is_system_config=True,
                create_person=user_entity,
                modify_person=user_entity,
            )
            ComponentAttributeValue.objects.create(
                componentidentifier=compound1,
                attribute=attribute_entity,
                main_value=json.dumps(compound_item["other_name"]),
            )


DataPresentWay_entity = DataPresentWay.objects.filter(
    name="Unknown").first()
if DataPresentWay_entity is None:
    DataPresentWay_entity = DataPresentWay.objects.create(
        name="Unknown",
        zh_name="未知",
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )


def store_sample(compound1, sample_json):
    nSampleNm = sample_json["nSampleNm"]
    if "eSource" in sample_json:
        eSource = sample_json["eSource"]
        eSource_entity = SampleSource.objects.filter(name=eSource).first()
        if eSource_entity is None:
            eSource_entity = SampleSource.objects.create(
                name=eSource,
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
    else:
        eSource_entity = SampleSource.objects.filter(name="unknown").first()
        if eSource_entity is None:
            eSource_entity = SampleSource.objects.create(
                name="unknown",
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
    sample_purity_list = []
    sample_purity_method_list = []
    for sample_purity_json in sample_json["purity"]:
        # 处理e纯化方法
        for epurity_method in sample_purity_json["ePurifMethod"]:
            purity_method_entity = Purification.objects.filter(
                name=epurity_method).first()
            if purity_method_entity is None:
                purity_method_entity = Purification.objects.create(
                    name=epurity_method,
                    create_person=user_entity,
                    modify_person=user_entity,
                )
            sample_purity_method_list.append(purity_method_entity)
        # 处理s纯化方法
        for spurity_method in sample_purity_json["sPurifMethod"]:
            purity_method_entity = Purification.objects.filter(
                name=spurity_method).first()
            if purity_method_entity is None:
                purity_method_entity = Purification.objects.create(
                    name=spurity_method,
                    comment="user enter",
                    create_person=user_entity,
                    modify_person=user_entity,
                )
            sample_purity_method_list.append(purity_method_entity)
        purity_determine_method = None
        if "eAnalMeth" in sample_purity_json:
            purity_determine_method = PurityDetermination.objects.filter(
                name=sample_purity_json["eAnalMeth"]).first()
            if purity_determine_method is None:
                purity_determine_method = PurityDetermination.objects.create(
                    name=sample_purity_json["eAnalMeth"],
                    create_person=user_entity,
                    modify_person=user_entity,
                )
        if "sAnalMeth" in sample_purity_json:
            purity_determine_method = PurityDetermination.objects.filter(
                name=sample_purity_json["sAnalMeth"]).first()
            if purity_determine_method is None:
                purity_determine_method = PurityDetermination.objects.create(
                    name=sample_purity_json["sAnalMeth"],
                    comment="user enter",
                    create_person=user_entity,
                    modify_person=user_entity,
                )
        purity_digit = 0
        if "nPurityMass" in sample_purity_json:
            purity_digit = sample_purity_json["nPurityMass"]

        other_json_data = sample_purity_json
        samplepurity1 = SamplePurity.objects.create(
            purity_determine_method=purity_determine_method,
            value=purity_digit,
            is_percentage=True,
            sample_purity_unit=percent_unit_entity,
            extra_properties=other_json_data,
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity,
        )
        sample_purity_list.append(samplepurity1)
    if len(sample_purity_list) == 0:
        init_purity_1 = SamplePurity.objects.create(
            value=0,
            is_percentage=True,
            sample_purity_unit=percent_unit_entity,
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity,
        )

        sample1 = Sample.objects.create(
            name=nSampleNm,
            source=eSource_entity,
            init_purity=init_purity_1,
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity,
        )
    else:
        sample1 = Sample.objects.create(
            name=nSampleNm,
            source=eSource_entity,
            init_purity=sample_purity_list[0],
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity,
        )
        sample1.purity_methods.set(sample_purity_method_list)
        sample1.final_puritys.set(sample_purity_list[1:])
        sample1.component = compound1
        sample1.save()
    return nSampleNm, sample1


def store_compound(compound_json):
    compound_dict = {}
    sample_dict = {}
    for compound_item in compound_json:
        regnum = compound_item["RegNum"]
        if "sStandardInChI" in compound_item:
            inchi = compound_item["sStandardInChI"]
        else:
            inchi = ""
        if "sStandardInChIKey" in compound_item:
            inchikey = compound_item["sStandardInChIKey"]
        else:
            inchikey = ""
        compound_name = compound_item["sCommonName"][0]
        compound_other_name = compound_item["sCommonName"][1:]
        if "sFormulaMolec" in compound_item:
            formula = compound_item["sFormulaMolec"]
        else:
            formula = ""
        compound1 = ComponentIdentifier.objects.filter(
            name=compound_name).first()
        if compound1 is None:
            compound1 = ComponentIdentifier.objects.create(
                name=compound_name,
                name_en=compound_name,
                molecular_formula=formula,
                inchi_number=inchi,
                smiles_number="",
                description="nist init",
                create_person=user_entity,
                modify_person=user_entity,
            )
            store_compound_attribute(
                compound1, inchikey=inchikey, other_name=compound_other_name)

        compound_dict[regnum] = compound1

        # 存储样品信息
        sample_dict[regnum] = {}
        sample_list = compound_item["Sample"]
        for sample in sample_list:
            sample_id, sample1 = store_sample(compound1, sample)
            sample_dict[regnum][sample_id] = sample1
    return compound_dict, sample_dict


def store_literature(literature_json):
    if "sTitle" in literature_json:
        title = literature_json["sTitle"]
    else:
        title = str(uuid.uuid4())
    if "yrPubYr" in literature_json:
        date = literature_json["yrPubYr"]
        # 将时间设置为data这年的第一天
        date = date + "-01-01"
    else:
        date = None
    if "sAbstract" in literature_json:
        abstract = literature_json["sAbstract"]
    else:
        abstract = ""
    if "sLanguage" in literature_json:
        Language_entity = Language.objects.filter(
            name=literature_json["sLanguage"]).first()
        if Language_entity is None:
            Language_entity = Language.objects.create(
                name=literature_json["sLanguage"],
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
    else:
        Language_entity = Language.objects.filter(
            name="unknown").first()
        if Language_entity is None:
            Language_entity = Language.objects.create(
                name="unknown",
                zh_name="未知",
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
    if "sDOI" in literature_json:
        DOI = literature_json["sDOI"]
    else:
        DOI = ""
    status_entity = LiteratureStatusType.objects.filter(
        name="Available").first()
    if status_entity is None:
        status_entity = LiteratureStatusType.objects.create(
            name="Available",
            zh_name="可用",
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity,
        )
    if "eType" in literature_json:
        liter_type_entity = LiteratureType.objects.filter(
            name=literature_json["eType"]).first()
        if liter_type_entity is None:
            liter_type_entity = LiteratureType.objects.create(
                name=literature_json["eType"],
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
    else:
        liter_type_entity = None
    if "sVol" in literature_json:
        volume = literature_json["sVol"]
    else:
        volume = ""
    if "sPage" in literature_json:
        pages = literature_json["sPage"].split("-")
        if len(pages) == 2:
            first_page = int(pages[0])
            last_page = int(pages[1])
        else:
            first_page = int(pages[0])
            last_page = int(pages[0])
    else:
        first_page = None
        last_page = None

    authors_list = []
    for author in literature_json["sAuthor"]:
        author_entity = Author.objects.filter(name=author).first()
        if author_entity is None:
            author_entity = Author.objects.create(
                name=author,
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
        authors_list.append(author_entity)
    keywords_list = []
    keywordtype_entity = KeywordType.objects.filter(
        name="nist").first()
    if keywordtype_entity is None:
        keywordtype_entity = KeywordType.objects.create(
            name="nist",
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity
        )
    for keyword in literature_json["sKeyword"]:
        if keyword == "" or keyword == None:
            continue
        keyword_entity = Keyword.objects.filter(k=keyword).first()
        if keyword_entity is None:
            keyword_entity = Keyword.objects.create(
                k=keyword,
                v=keyword,
                kt=keywordtype_entity,
                is_reviewing=True,
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
        keywords_list.append(keyword_entity)
    literature1 = Literature.objects.create(
        title=title,
        zh_title=title,
        date=date,
        abstract=abstract,
        language=Language_entity,
        DOI=DOI,
        status=status_entity,
        liter_type=liter_type_entity,
        net_src_state=False,
        pub_type="unknown",
        volume=volume,
        first_page=first_page,
        last_page=last_page,
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
        extra_properties=literature_json,
    )
    literature1.authors.set(authors_list)
    literature1.keywords.set(keywords_list)
    return literature1


def store_data_set(dataset_json, compound_dict, sample_dict, literature):
    # 定义数据集
    dataset_name = str(uuid.uuid4())
    if "eExpPurpose" in dataset_json:
        purpose_entity = ExpPurpose.objects.filter(
            name=dataset_json["eExpPurpose"]).first()
        if purpose_entity is None:
            purpose_entity = ExpPurpose.objects.create(
                name=dataset_json["eExpPurpose"],
                creator=user_entity,
                last_editor=user_entity,
                reviewer=user_entity,
            )
    else:
        purpose_entity = None

    # 设置constraint
    constraint_list = []
    for constraint_content in dataset_json["Constraint"]:
        constraint_label = ""
        constraint_unit = ""
        if "ConstraintIDType" in constraint_content:
            constraint_label += "type:" + \
                constraint_content["ConstraintIDType"] + " ; "
        if "ConstraintID" in constraint_content:
            constraint_label += "id:" + \
                constraint_content["ConstraintID"] + " ; "
            constraint_id_list = constraint_content["ConstraintID"].split(",")
            if len(constraint_id_list) >= 2:
                constraint_unit = constraint_id_list[-1]
        if "ConstraintPhaseID" in constraint_content:
            constraint_label += "phase:" + \
                constraint_content["ConstraintPhaseID"] + " ; "
        constraint_has_value = True
        value_type = "float"
        if "nConstraintValue" in constraint_content:
            constraint_value = constraint_content["nConstraintValue"]
        else:
            constraint_value = "unknown"
        constraint1 = Constraint.objects.create(
            label=constraint_label,
            has_value=constraint_has_value,
            value_type=value_type,
            value=constraint_value,
            unit=constraint_unit,
            creator=user_entity,
            last_editor=user_entity,
            reviewer=user_entity,
        )
        constraint_list.append(constraint1)
    # 设置相态
    phases_list = []
    pheses_set = set()
    for phase_content in dataset_json["PhaseID"]:
        phase_name = phase_content["ePhase"]
        if phase_name in pheses_set:
            continue
        phase_entity = Phase.objects.filter(name=phase_name).first()
        if phase_entity is None:
            phase_entity = Phase.objects.create(
                name=phase_name,
                is_pure_used=True,
                is_binary_used=True,
                is_ternary_used=True,
                is_reaction_used=True,
                is_complex_used=True,
                create_person=user_entity,
                modify_person=user_entity,
            )
        phases_list.append(phase_entity)
    expdataset_entity = ExpDataset.objects.create(
        name=dataset_name,
        zh_name=dataset_name,
        source_liter=literature,
        purpose=purpose_entity,
        data_present_way=DataPresentWay_entity,
        mixture=None,
        creator=user_entity,
        last_editor=user_entity,
        reviewer=user_entity,
    )
    # expdataset_entity.components.set(components_list)
    expdataset_entity.constraint.set(constraint_list)
    expdataset_entity.phases.set(phases_list)
    
    # components_list = []
    for component_content in dataset_json["Component"]:
        if "RegNum" in component_content:
            regnum = component_content["RegNum"]
            if "nSampleNm" in component_content:
                nSampleNm = component_content["nSampleNm"]
                comp = compound_dict[regnum]
                sample = sample_dict[regnum][nSampleNm]
                ExpDatasetComponentMap.objects.create(exp_dataset=expdataset_entity,
                    component = comp,
                    sample = sample
                )
    
    # 设置数据点
    # 构造物性
    property_id_property_dict = {}
    variable_id_variable_dict = {}
    for property_content in dataset_json["Property"]:
        nPropNumber = property_content["nPropNumber"]
        if "PropertyMethodID" in property_content:
            property_c1 = property_content["PropertyMethodID"]
            property_group1 = PropertyGroup.objects.filter(
                name=property_c1["PropertyGroup"]).first()
            if property_group1 is None:
                property_group1 = PropertyGroup.objects.create(
                    name=property_c1["PropertyGroup"],
                    is_pure_used=True,
                    is_binary_used=True,
                    is_ternary_used=True,
                    is_reaction_used=True,
                    is_complex_used=True,
                    create_person=user_entity,
                    modify_person=user_entity,
                )
            property_name_list = property_c1["ePropName"].split(",")
            if len(property_name_list) >= 2:
                unit_1_entity = Unit.objects.filter(
                    symbol=property_name_list[-1]).first()
                if unit_1_entity is None:
                    unit_1_entity = Unit.objects.create(
                        unit_name=property_name_list[-1],
                        unit_name_en=property_name_list[-1],
                        symbol=property_name_list[-1],
                        is_system = True,
                        dimension=dimension_less_entity,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    UnitSymbol.objects.create(
                        symbol=property_name_list[-1],
                        root_unit=unit_1_entity,
                        is_system = True,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    UnitConversion.objects.create(
                        base_unit=unit_entity,
                        target_unit=unit_1_entity,
                        factor=1,
                        offset=0,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    QuantityUnitMap.objects.create(
                        quantity=quantity_entity,
                        unit=unit_1_entity,
                    )
                property_name = "".join(property_name_list[:-1])
            else:
                unit_1_entity = unit_entity
                property_name = property_c1["ePropName"]
            property1 = Property.objects.filter(
                name=property_name).first()
            if property1 is None:
                property1 = Property.objects.create(
                    name=property_name,
                    name_en=property_name,
                    property_group=property_group1,
                    quantity=quantity_entity,
                    common_used=True,
                    is_single_value=True,
                    is_fixed_value=True,
                    is_one_variable=True,
                    is_two_variable=True,
                    is_mixz=True,
                    is_mixv=True,
                    is_reaction=True,
                    is_need_solvent_binary=True,
                    is_need_solvent_ternary=True,
                    create_person=user_entity,
                    modify_person=user_entity,
                )
            # 物性实验方法
            if "eMethodName" in property_c1:
                property_method_entity = Method.objects.filter(
                    name=property_c1["eMethodName"]).first()
                if property_method_entity is None:
                    property_method_entity = Method.objects.create(
                        name=property_c1["eMethodName"],
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                # 物性实验方法和物性的关系
                if MethodPropertyMap.objects.filter(method=property_method_entity, property=property1).first() is None:
                    property_method_property_map_entity = MethodPropertyMap.objects.create(
                        method=property_method_entity,
                        property=property1,
                    )
            if "sMethodName" in property_c1:
                property_method_entity = Method.objects.filter(
                    name=property_c1["sMethodName"]).first()
                if property_method_entity is None:
                    property_method_entity = Method.objects.create(
                        name=property_c1["sMethodName"],
                        comment="user enter",
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                # 物性实验方法和物性的关系
                if MethodPropertyMap.objects.filter(method=property_method_entity, property=property1).first() is None:
                    property_method_property_map_entity = MethodPropertyMap.objects.create(
                        method=property_method_entity,
                        property=property1,
                    )

        expdatasetproperty_entity = ExpDatasetPropertyMap.objects.create(
            exp_dataset=expdataset_entity,
            property=property1,
            unit=unit_1_entity,
            expect_uncertainty=0,
            exp_unc_percent=False,
            variable_uncertainty=0,
            var_unc_percent=False,
            combine_uncertainty=0,
            com_unc_percent=False,
            data_definition_way=None,
            ref_temp=None,
            ref_press=None,
            ref_phase=None,
            measurement_error=0,
            measurement_error_percent=False,
            systematic_error=0,
            systematic_error_percent=False,
            random_error=0,
            random_error_percent=False,
        )
        property_id_property_dict[nPropNumber] = expdatasetproperty_entity
        # 变量
        for variable_content in dataset_json["Variable"]:
            nVarNumber = variable_content["nVarNumber"]
            variable_name_list = variable_content["VariableID"].split(",")
            if len(variable_name_list) >= 2:
                unit_2_entity = Unit.objects.filter(
                    symbol=variable_name_list[-1]).first()
                if unit_2_entity is None:
                    unit_2_entity = Unit.objects.create(
                        unit_name=variable_name_list[-1],
                        unit_name_en=variable_name_list[-1],
                        symbol=variable_name_list[-1],
                        is_system = True,
                        dimension=dimension_less_entity,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    UnitSymbol.objects.create(
                        symbol=variable_name_list[-1],
                        root_unit=unit_2_entity,
                        is_system = True,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    UnitConversion.objects.create(
                        base_unit=unit_entity,
                        target_unit=unit_2_entity,
                        factor=1,
                        offset=0,
                        create_person=user_entity,
                        modify_person=user_entity,
                    )
                    QuantityUnitMap.objects.create(
                        quantity=quantity_entity,
                        unit=unit_2_entity,
                    )
                variable_name = "".join(variable_name_list[:-1])
            else:
                unit_2_entity = unit_entity
                variable_name = variable_content["VariableID"]
            if "VariableIDType" in variable_content:
                variable_name = variable_content["VariableIDType"] + \
                    ";" + variable_name
            # if "VariableIDRegNum" in variable_content:
            #     variable_name = variable_name + ";" + \
            #         variable_content["VariableIDRegNum"]
            if "VarPhaseID" in variable_content:
                variable_name = variable_name + ";" + \
                    variable_content["VarPhaseID"]
            variable_1_entity = PropertyVariable.objects.filter(
                name=variable_name).first()
            if variable_1_entity is None:
                variable_1_entity = PropertyVariable.objects.create(
                    name=variable_name,
                    name_en=variable_name,
                    quantity=quantity_entity,
                    pure_used=True,
                    binary_used=True,
                    ternary_used=True,
                    constrain_used=True,
                    component_related=True,
                    need_solvent_binary=False,
                    need_solvent_ternary=False,
                    create_person=user_entity,
                    modify_person=user_entity,
                )
            expdatesetvariable_entity = ExpDatasetVariableMap.objects.create(
                exp_dataset=expdataset_entity,
                variable=variable_1_entity,
                unit=unit_2_entity,
            )
            variable_id_variable_dict[nVarNumber] = expdatesetvariable_entity
    # 绑定物性和变量
    for property_id in property_id_property_dict:
        for variable_id in variable_id_variable_dict:
            if PropertyPropertyVariableMap.objects.filter(property=property_id_property_dict[property_id].property, property_variable=variable_id_variable_dict[variable_id].variable).first() is None:
                PropertyPropertyVariableMap.objects.create(
                    property=property_id_property_dict[property_id].property,
                    property_variable=variable_id_variable_dict[variable_id].variable,
                )

    # 构造数据点
    data_point_list = []
    for data_content in dataset_json["NumValues"]:
        data_point_1 = DataPoint.objects.create()
        for variable_content in data_content["VariableValue"]:
            DataPointExpDatasetVariableMap.objects.create(
                data_point=data_point_1,
                exp_dataset_variable_map=variable_id_variable_dict[variable_content["nVarNumber"]],
                value=float(variable_content["nVarValue"]),
            )
        for property_content in data_content["PropertyValue"]:
            DataPointExpDatasetPropertyMap.objects.create(
                data_point=data_point_1,
                exp_dataset_property_map=property_id_property_dict[property_content["nPropNumber"]],
                value=float(property_content["nPropValue"]),
            )
        data_point_list.append(data_point_1)
    expdataset_entity.data_points.set(data_point_list)
    return expdataset_entity


def insert_single_nist_file(file_path):
    processed_data_json = json.load(open(file_path, "r",encoding="utf-8"))
    # 存储组分
    compound_dict, sample_dict = store_compound(
        processed_data_json["Compound"])
    # 存储文献
    literature = store_literature(processed_data_json["Citation"])
    # 存储数据点
    dataset_list = []
    for dataset_json in processed_data_json["PureOrMixtureData"]:
        dataset1 = store_data_set(
            dataset_json, compound_dict, sample_dict, literature)
        dataset_list.append(dataset1)


def init_nist_data_v2():
    # 路径配置
    # TODO: 需要修改为实际路径
    data_dir_path = r"D:\清华\硕士\物性数据库\开发\code\dev\pro-backend\files\nist_dataV2"
    if not os.path.exists(data_dir_path):
        print("Data directory not found!", data_dir_path)
        return
    data_storate_path = os.path.join(data_dir_path, "storage_data")
    if not os.path.exists(data_storate_path):
        print("Data storage directory not found!", data_storate_path)
        return
    # 记录数据文件夹
    backend_file_path = "./files"
    if not os.path.exists(backend_file_path):
        os.mkdir(backend_file_path)
    nist_dir_path = os.path.join(backend_file_path, "nist_dataV2")
    if not os.path.exists(nist_dir_path):
        os.mkdir(nist_dir_path)
    # 获取data_dir_path最后一个文件夹的名称
    nist_data_version = os.path.basename(data_dir_path)
    nist_data_file_path = os.path.join(nist_dir_path, nist_data_version)
    if not os.path.exists(nist_data_file_path):
        os.mkdir(nist_data_file_path)
    nist_log_file = os.path.join(nist_data_file_path, "nist_log.log")
    if os.path.exists(nist_log_file):
        os.remove(nist_log_file)

    # 获取文件夹里的所有文件
    files = os.listdir(data_storate_path)
    num = 0
    for file in tqdm(files, desc="Processing NIST data"):
        file_path = os.path.join(data_storate_path, file)
        try:
            num += 1
            # if num >1:
            #     break
            # if num % 3 == 1 or num % 3 == 2:
            #     continue
            # if num > 300:
            #     break
            insert_single_nist_file(file_path)

        except Exception as e:
            # print(file_path)
            # traceback.print_exc()
            exc_str = traceback.format_exc()
            # return
            with open(nist_log_file, "a",encoding="utf-8") as f:
                error_message = "Error file: " + file_path + \
                    "\n" + exc_str + "\n" + "-"*50 + "\n"
                f.write(error_message)
